﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Search : BasePage
    {
        int SID = 0;
        int Top = 20;
        int Orderby = 2;
        string Keyword = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["keyword"]!=null)
            {
                Keyword = Session["keyword"].ToString();
                Page.Title = new sysConfigBL().SelectByPK(1).Rows[0]["ConfigValue"].ToString();
                if (Session["lang"].ToString() == "VN") SID = 0;
                if (Session["lang"].ToString() == "EN") SID = 1;

                rptproDataSource(Keyword, Top, Orderby, SID);
                if (!IsPostBack)
                {
                    ddlSortDataSource();
                }
            }

        }

        protected void rptproDataSource(string Keyword, int Top, int OrderBy, int Sid)
        {
            CollectionPager1.MaxPages = 10000;

            CollectionPager1.PageSize = Top; // số items hiển thị trên một trang

            CollectionPager1.DataSource = new cmsSanPhamBL().SearchByName(Keyword,Top, Orderby, Sid).DefaultView; ;

            CollectionPager1.BindToControl = rptpro;
            rptpro.DataSource = CollectionPager1.DataSourcePaged;
            // rptpro.DataSource = new cmsSanPhamBL().SelectAllChildrenProducts(LoaiSPID, Top, Orderby, Sid);
            rptpro.DataBind();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        protected void lbtn20_Click(object sender, EventArgs e)
        {
            Top = 20;
            rptproDataSource(Keyword,Top, Orderby, SID);
        }
        protected void lbtn50_Click(object sender, EventArgs e)
        {
            Top = 50;
            rptproDataSource(Keyword, Top, Orderby, SID);
        }
        protected void lbtn100_Click(object sender, EventArgs e)
        {
            Top = 100;
            rptproDataSource(Keyword, Top, Orderby, SID);

        }

        protected void ddlSortDataSource()
        {
            if (Session["lang"].ToString() == "VN")
            {
                ddlSortBy.Items.Clear();
                ListItem vlst1 = new ListItem("Tên sản phẩm(Tăng dần)", "0");
                ListItem vlst2 = new ListItem("Tên sản phẩm(Giảm dần)", "1");
                ListItem vlst3 = new ListItem("Giá sản phẩm(Tăng dần)", "2");
                ListItem vlst4 = new ListItem("Giá sản phẩm(Giảm dần)", "3");
                ddlSortBy.Items.Add(vlst1);
                ddlSortBy.Items.Add(vlst2);
                ddlSortBy.Items.Add(vlst3);
                ddlSortBy.Items.Add(vlst4);
            }
            if (Session["lang"].ToString() == "EN")
            {
                ddlSortBy.Items.Clear();
                ListItem vlst1 = new ListItem("Product name(Ascending)", "0");
                ListItem vlst2 = new ListItem("Product name(Descending)", "1");
                ListItem vlst3 = new ListItem("Price(Ascending)", "2");
                ListItem vlst4 = new ListItem("Price(Descending)", "3");
                ddlSortBy.Items.Add(vlst1);
                ddlSortBy.Items.Add(vlst2);
                ddlSortBy.Items.Add(vlst3);
                ddlSortBy.Items.Add(vlst4);
            }
        }

        protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            Orderby = int.Parse(ddlSortBy.SelectedValue);
            rptproDataSource(Keyword, Top, Orderby, SID);

        }
    }
}
